package org.jet.emall.prod.controller;

import io.swagger.annotations.ApiOperation;
import org.jet.emall.common.restful.result.CommonPage;
import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.log.AdminOperationLog;
import org.jet.emall.log.LogType;
import org.jet.emall.prod.dto.OrderDTO;
import org.jet.emall.prod.service.CustomOrderService;
import org.jet.emall.prod.transfer.OrderVoTransfer;
import org.jet.emall.prod.vo.OrderVO;
import org.jet.emall.prod.wrapper.OrderCodeAndStatusWrapper;
import org.jet.emall.rbac.annotation.ApiInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-05-02 11:55
 */
@RestController
public class OrderController {

    @Autowired
    private CustomOrderService customOrderService;

    @ApiInformation(function = "分页查找订单",menu = "订单列表")
    @ApiOperation(value="分页查找订单")
    @RequestMapping(value = "/orders",method = RequestMethod.GET)
    public CommonResult<CommonPage<OrderVO>> page(String code, String shippingUser, String shippingPhone, String shippingAddress, Byte status, Long minMoney, Long maxMoney, Integer pageNum, Integer pageSize){
        CommonPage<OrderDTO> page = customOrderService.page(code,shippingUser,shippingPhone,shippingAddress,status,minMoney,maxMoney,pageNum,pageSize);
        CommonPage<OrderVO> commonPage = (CommonPage<OrderVO>)(Object)page;
        commonPage.setList(OrderVoTransfer.INSTANCE.toVoList(page.getList()));
        return CommonResult.success(commonPage,"分页查找订单成功");
    }


    @ApiInformation(function = "获取订单详情",menu = "订单列表")
    @ApiOperation(value="获取订单详情")
    @RequestMapping(value = "/orders/{id}",method = RequestMethod.GET)
    public CommonResult<OrderVO> getOrder(@PathVariable Long id){
        OrderDTO order = customOrderService.getOrderById(id);
        OrderVO orderVO = OrderVoTransfer.INSTANCE.toVoWithItems(order);
        return CommonResult.success(orderVO,"获取订单详情成功");
    }


    @ApiInformation(function = "删除订单",menu = "订单列表")
    @ApiOperation(value="删除订单")
    @AdminOperationLog(value = "删除订单",logType = LogType.DELETE)
    @RequestMapping(value = "/orders",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteOrder(Long id){
        customOrderService.delete(id);
        return CommonResult.deleted("删除订单成功");
    }

    @ApiInformation(function = "修改订单状态",menu = "订单列表")
    @ApiOperation(value="修改订单状态")
    @AdminOperationLog(value = "修改订单状态",logType = LogType.UPDATE)
    @RequestMapping(value = "/orders/status",method = RequestMethod.PATCH)
    public CommonResult<Object> updateOrderStatus(@RequestBody @Valid OrderCodeAndStatusWrapper wrapper){
        customOrderService.update(wrapper.getCode(),wrapper.getStatus());
        return CommonResult.success("修改订单状态成功");
    }
}
